package net.javlov.world.grid;

import java.util.Arrays;

import net.javlov.Action;
import net.javlov.Agent;
import net.javlov.Function;
import net.javlov.RewardFunction;
import net.javlov.State;
import net.javlov.world.Body;
import net.javlov.world.CollisionEvent;
import net.javlov.world.CollisionListener;

public class GridRewardFunction implements CollisionListener, RewardFunction {

	protected GridWorld.RewardBroker broker;
	
	protected double reward, shapingReward, lastPhi = Double.NaN;
	
	protected boolean saShaping;
	
	protected Function shapingFunction;
	
	public GridRewardFunction() {
		shapingFunction = new ZeroFunction();
	}
	
	public void init() {}
	
	public void setRewardBroker(GridWorld.RewardBroker b) {
		broker = b;
	}
	
	@Override
	public void preAction(Action a, Agent agent, State s) {
		reward = 0;
		rewardPreAction(a, agent, s);
		shapingPreAction(a, agent, s);
	}
	
	@Override
	public double calculateReward(State sprime) {
		setReward(sprime);
		setShapingReward(sprime);
		return reward + shapingReward;
	}

	@Override
	public void collisionOccurred(CollisionEvent e) {
		//TODO assuming body1 belongs to agent that is currently executing action and will
		//thus receive reward; is this valid assumption?
		reward += e.getBody2().getReward();
		if ( e.getBody2().getType() == Body.GOAL )
			broker.endEpisode();
	}
	
	public Function getShapingFunction() {
		return shapingFunction;
	}

	/**
	 * Appends a shaping function to the base reward function. The boolean parameter indicates
	 * whether the shaping function is a potential over states (false) or over state,action
	 * pairs (true).
	 * 
	 * @param shapingFunction the potential function
	 * @param stateActionShaping indicates
	 * whether the shaping function is a potential over states (false) or over state,action
	 * pairs (true).
	 */
	public void setShapingFunction(Function shapingFunction, boolean stateActionShaping) {
		this.shapingFunction = shapingFunction;
		saShaping = stateActionShaping;
	}
	
	/*** FUNCTIONS FOR IMMEDIATE REWARD TO OVERRIDE ***/
	protected void rewardPreAction(Action a, Agent agent, State s) {
		reward = -1;
	}
	
	protected void setReward(State sprime) {}
	
	/*** FUNCTIONS FOR SHAPING ***/
	protected void shapingPreAction(Action a, Agent agent, State s) {
		if ( Double.isNaN(lastPhi) ) {//only eval first time it's called
			lastPhi = shapingFunction.eval(getStateDataForShaping(s, a))[0];
		} else if ( saShaping ) {
			double d = shapingFunction.eval(getStateDataForShaping(s, a))[0];
			shapingReward = d - lastPhi;
			lastPhi = d;
		}
	}
	
	protected void setShapingReward(State sprime) {
		if ( !saShaping ) {
			double prevLastPhi = lastPhi;
			lastPhi = shapingFunction.eval(getStateDataForShaping(sprime,null))[0];
			shapingReward = lastPhi - prevLastPhi;
		}
		if ( sprime.isTerminal() )
			lastPhi = Double.NaN;
	}
	
	protected double[] getStateDataForShaping(State s, Action a) {
		double[] d = (double[])s.getData();
		if ( saShaping ) {
			d = Arrays.copyOf(d, d.length+1);
			d[d.length-1] = a.getID();
		}
		return d;
	}
	
	private static class ZeroFunction implements Function {

		static double[] ret = new double[1];
		
		@Override
		public double[] eval(double[] x) {
			return ret;
		}
		
	}

}
